package classes.view.component 
{
	import classes.controller.DAOSendFlash;
	import classes.controller.DAOSendJS;
	import classes.controller.EDataObject;
	import classes.controller.ESendJS;
	import classes.controller.VOLoad;
	import classes.model.VOIsLocal;
	
	import com.d4p.custom.customScrollBar.CustomScrollBar;
	import com.d4p.custom.customScrollBar.Scrollbar;
	import com.greensock.TweenMax;
	import com.greensock.easing.Cubic;
	
	import flash.display.MovieClip;
	import flash.display.Sprite;
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.URLLoader;
	import flash.net.URLRequest;
	
	public class PopupSelect_DBList extends Sprite
	{
		private var _this_obj:Object;
		private var _list_mc:MovieClip;
		//private var _scroll_mc:Scrollbar;
		private var _select_mc:MovieClip;
		private var _con_mc:MovieClip;
		private var _input_mc:MovieClip;
		private var _dataLoad_chk:Boolean = false;
		private var _thisOver_chk:Boolean = false;
		private var _thisOpen_chk:Boolean = false;
		private var _thisActiveView_chk:Boolean = false;
		
		private var _customScrollBar:CustomScrollBar;
		private var _view_title_str:String;
		
		
		public function PopupSelect_DBList() 
		{
			super();
			setLayOut();
			initListener();
			defaultSetting();
		}
		
		private function setLayOut():void
		{
			this._select_mc	=	this.getChildByName('select_mc') as MovieClip;
			this._input_mc	=	this.getChildByName('input_mc') as MovieClip;
		}
		
		private function initListener():void
		{
			this._select_mc.addEventListener(MouseEvent.ROLL_OVER, listOver);
			this._select_mc.addEventListener(MouseEvent.ROLL_OUT, listOver);
			
			this._input_mc.buttonMode = true;
			this._input_mc.addEventListener(MouseEvent.CLICK, inputClick);
			
			this.addEventListener(MouseEvent.ROLL_OVER, thisOver);
			this.addEventListener(MouseEvent.ROLL_OUT, thisOut);
		}
		
		private function thisOver(e:MouseEvent):void 
		{
		//	_scroll_mc.wheelStart();
			_thisOver_chk = true;
		}
		
		private function thisOut(e:MouseEvent):void 
		{
		//	_scroll_mc.wheelEnd();
			_thisOver_chk = false;
		}
		private function defaultSetting():void
		{
			active_txt.mouseWheelEnabled = false;
			title_txt.mouseWheelEnabled = false;
			active_txt.mouseEnabled = false;
			title_txt.mouseEnabled = false;
			
			_view_title_str = title_txt.text;
			
		//	_scroll_mc = new Scrollbar();
			//addChild(_scroll_mc);
		//	this._select_mc.scroll_mc.visible = false;
			
			_con_mc = new MovieClip();
			this._select_mc.contents_mc.addChild(_con_mc);
			
			//_con_mc.mask = _select_mc.mask_mc;
			
			TweenMax.to(_select_mc, 0, { autoAlpha:0 } );
		}
		
		
		private function inputClick(e:Event):void 
		{
			if (!_dataLoad_chk) {
				_dataLoad_chk = true;
				this.data_init();
			} else {
				if (_thisOpen_chk) {
					this.viewOFF();
				} else {
					trace("-----------");
					this.viewON();
				}
				
			}
		}
		
		public var data_str:String = "";
		public var data_event_str:String = "";
		public var data_event:String;
		public var data_obj:Object;
		
		
		public function data_init():void
		{
			if (data_str == "") {
				_this_obj = data_obj;
				on();
			} else {
				
				if(VOIsLocal.getLocalInstance().isLocal == "FILE"){
					var loader:URLLoader = new URLLoader();
					loader.addEventListener( Event.COMPLETE,	onDataLoaded);
					try {
						loader.load( new URLRequest(data_str));
					}catch ( error:Error ) {
						trace( "Unable to load requested document." );
					}
				}else {
					//real.
					DAOSendFlash.getLoadInstance().setData(data_event_str, "");
					DAOSendJS.getLoadInstance().addEventListener(data_event, onRoomInfoLoadcomplete);
				}
				//VOLoad.getLoadInstance().addEventListener(EDataObject.EVENT_DATAOBJECT, loadComplete);
				//VOLoad.getLoadInstance().load_list(data_str, 4);
			}
			
		}
		
		private function onDataLoaded(e:Event):void
		{
			var jsonParsed:*  = JSON.parse(e.target.data);
			_this_obj 	= jsonParsed;
			
			loadComplete();
		}
		
		private function onRoomInfoLoadcomplete(e:ESendJS):void
		{
			var obj:Object 	= e._obj;
			_this_obj = obj;
			
			loadComplete();
		}
		
		private function loadComplete():void
		{
			//VOLoad.getLoadInstance().removeEventListener(EDataObject.EVENT_DATAOBJECT, loadComplete);
			//
			//_this_obj 	= e._object;
			//_paging 		= obj.data.nowpage;
			//_totalPage 		= obj.data.totalpage;
			
			on();
		}
		
		public  function set viewTitle($title):void
		{
			_view_title_str = $title;
			title_txt.text = $title;
		}
		
		public  function set viewWidth($num):void
		{
			input_mc.c_mc.width = $num - 9 - 6 - input_mc.r_mc.width + 12;
			input_mc.r_mc.x = input_mc.c_mc.x + input_mc.c_mc.width;
			mask_mc.width = $num + 10;
			_select_mc.bg_mc.width = $num - 2;
		}
		
		public function set viewActive($chk):void
		{
			_thisActiveView_chk = $chk
		}
		
		private function childMake(i, $data) {
			var child_mc:PopupSelectItem = new PopupSelectItem();
			child_mc.y = i* 24;
			child_mc.text_txt.width = _select_mc.bg_mc.width - 5;
			child_mc.line_mc.width = _select_mc.bg_mc.width;
			child_mc.text_txt.text = $data;
			child_mc.num = i;
			
			if (i == 0) {
				child_mc.line_mc.visible = false;
			}
			
			if (total_num > 4) {
				child_mc.line_mc.width = _select_mc.bg_mc.width - 26;
			} else {
				child_mc.line_mc.width = _select_mc.bg_mc.width;
			}
			child_mc.addEventListener(MouseEvent.CLICK, childClick);
			_con_mc.addChild(child_mc);
		}
		
		private var total_num = 0;
		public function on():void
		{
			total_num = _this_obj.length;
			//childMake(0, title_txt.text);
			
			for (var i in _this_obj) {
				var data  = _this_obj[i].name;
				i++;
				childMake(i,data);
			}
			
			_select_mc.mask_mc.width = _select_mc.bg_mc.width;
			
			if (i > 3) {
				i = 3;
				_select_mc.mask_mc.width = _select_mc.bg_mc.width - 26;
			}
			_select_mc.bg_mc.height = (i + 1) * 24 + 5;
			_select_mc.mask_mc.height = _select_mc.bg_mc.height;
			_select_mc.y = 30 - _select_mc.bg_mc.height;
			
			_select_mc.bar_mc.x = _select_mc.btn_mc.x = _select_mc.bg_mc.width - 19;
			
			//_select_mc.scroll_mc.x = _select_mc.bg_mc.width - 19;
			//_scroll_mc.init(_select_mc.scroll_mc, this, _con_mc, _con_mc.height, _select_mc.mask_mc.height, null, 1, null, 0, 10, true);
			
			trace('>>>>'+i);
			
			_customScrollBar = new CustomScrollBar(root.stage, _select_mc.contents_mc, _select_mc.mask_mc, _select_mc.bar_mc,  _select_mc.btn_mc, true, true, true, false, .15, 5);
			
			
			
			viewON();
		}
		
		private function childClick(e:MouseEvent):void 
		{
			var target_mc:PopupSelectItem = e.currentTarget as PopupSelectItem;
			
			viewOFF();
			
			if(_thisActiveView_chk) {
				input_mc.l_mc.gotoAndStop(2);
				input_mc.c_mc.gotoAndStop(2);
				input_mc.r_mc.gotoAndStop(2);
				//title_txt.textColor = 0xffffff;
				active_txt.text = _this_obj[target_mc.num].name;
				active_txt.visible = true;
				title_txt.visible = false;
			} else {
				active_txt.visible = false;
				title_txt.visible = true;
			}
			
			if (target_mc.num == 0) {
				title_txt.text = _view_title_str;
			} else {
				title_txt.text = _this_obj[target_mc.num].name;
			}
			
			
			dispatchEvent(new EDataObject([_this_obj[target_mc.num]], EDataObject.EVENT_DATAOBJECT));
		}
		
		private function stageDown(e:MouseEvent):void 
		{
			if (!_thisOver_chk) {
				viewOFF();
			}
		}
		private function viewON():void
		{
			_thisOpen_chk = true;
			TweenMax.to(_select_mc, .2, { autoAlpha:1, y:30, ease:Cubic.easeOut } );
			stage.addEventListener(MouseEvent.MOUSE_DOWN, stageDown);
		}
		
		private function viewOFF():void
		{
			_thisOpen_chk = false;
			stage.removeEventListener(MouseEvent.MOUSE_DOWN, stageDown);
			TweenMax.to(_select_mc, .2, { autoAlpha:0, y:30  -_select_mc.bg_mc.height, ease:Cubic.easeOut } );
		}
		
		
	//_scroll_mc.init(scroll_mc, this, con_mc, con_mc.height, _mask_mc.height, listScroll, 0, null, 22, 10, true);
	//_scroll_mc.scrollReset(con_mc, con_mc.height, _mask_mc.height, listScroll, 0);
		
		private function listOver(e:MouseEvent):void 
		{
			switch (e.type) 
			{
				case "rollOver":
				//	_scroll_mc.wheelStart();
					break;
				case "rollOut":
			//		_scroll_mc.wheelEnd();
					break;
			}
		}
	}

}